/*
 * The MIT License
 *
 * Copyright 2014 David Rodenas Pico.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package edu.uoc.sd.fr14.model;

import java.io.Serializable;

/**
 * This class unifies all the state of the server, including:
 * - the application state itself (company)
 * - the operations log
 * - the tracker of other server nseq (applied log operations)
 * - the role of the current server.
 * <p>
 * 
 * Please, do not confuse with application state, which is just the company.
 * In that case, each server has extra state in order to synchronize all applications states
 * successfully.
 * 
 * 
 * @author drodenas
 */
public final class ServerState implements Serializable {
	private static final long serialVersionUID = -7940837356767000914L;
	protected ServerState() { this.company = null; this.log = null; this.nseqTracker = null; this.role= null; }
	
	private final Company company;
    private final Log log;
    private final NseqTracker nseqTracker;
    private final Role role;

    /**
     * You should NEVER call to this function.
     * This is used only for testing
     * @param serverId
     */
    public ServerState(ServerId serverId) {
        if (serverId == null) throw new NullPointerException("Illegal argument: serverId cannot be null");
        
        this.company = new Company(serverId);
        this.log = new Log(serverId);
        this.nseqTracker = new NseqTracker(serverId);
        this.role = new Role(serverId);
    }

    /**
     * Gets the application state, the company.
     * <p>
     * 
     * You should only use this method to query the company and its flights.
     * You should use the Log ( {@link Log#executeOp(Op, Company)} ) and
     * SubLog ( {@link SubLog#apply(Company)} ) to modify it.
     * 
     * @return the company, never null.
     */
    public Company getCompany() {
        return company;
    }

    /**
     * Gets the log of the current server.
     * 
     * @return the log, never null
     */
    public Log getLog() {
        return log;
    }

    /**
     * Gets the nseq commited operations in other server logs tracker.
     * 
     * @return the nseqTracker, never null
     */
    public NseqTracker getNseqTracker() {
        return nseqTracker;
    }

    /**
     * Gets the role of the current server.
     * 
     * @return the role, never null
     */
    public Role getRole() {
        return role;
    }

    @Override
    public String toString() {
        return "ServerState{" + "company=" + company + ", log=" + log + ", nseqTracker=" + nseqTracker + ", role=" + role + '}';
    }
    
}
